package com.ruoyi.web.service;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

@Service
public class C2ReleaseVersionOverviewService {

    @Autowired
    private DruidDataSource druidDataSource;

    /**
     * 统计计划版本数、释放版本数和发布失败数
     *
     * @return 返回包含计划版本数、释放版本数和发布失败数的统计结果
     */
    public Map<String, Integer> getVersionFunnelData() {
        String sql = "SELECT " +
                "COUNT(*) AS totalVersions, " +
                "SUM(CASE WHEN releaseStatus IN (1) THEN 1 ELSE 0 END) AS releasedVersions, " +
                "SUM(CASE WHEN pubStatus IN (0) THEN 1 ELSE 0 END) AS publishVersions " +
                "FROM c2_version_info;";

        Map<String, Integer> result = new HashMap<>();
        try (Connection conn = druidDataSource.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql);
             ResultSet rs = stmt.executeQuery()) {

            if (rs.next()) {
                result.put("totalVersions", rs.getInt("totalVersions")); // 计划版本数
                result.put("releasedVersions", rs.getInt("releasedVersions")); // 释放版本数
                result.put("publishVersions", rs.getInt("publishVersions")); // 发布版本数
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }
}
